using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_ChooseGiftSlider : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_ChooseGiftSlider
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExText textUseNum { protected set; get; }
            public IQIGame.Onigao.Framework.ExSlider slider { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnAdd { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnMinus { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textUseNum", out var __tbv0);
                this.textUseNum = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExSlider>("slider", out var __tbv1);
                this.slider = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnAdd", out var __tbv2);
                this.btnAdd = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnMinus", out var __tbv3);
                this.btnMinus = __tbv3;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private string strFmtNum;
        private NumerCappedRange limitNum;
        private System.Action<NumerCappedRange> funcNumChanged;
        #endregion fields

        #region properties

        protected UIB_UIN_ChooseGiftSlider ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_ChooseGiftSlider();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.slider.onValueChanged.AddListener(this.OnSliderValueChanged);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {
            this.ClearData();
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.btnAdd.onClick.AddListener(this.OnClickAdd);
            this.ui.btnMinus.onClick.AddListener(this.OnClickReduce);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds
        public void Show(string strFmtNum, NumerCappedRange limitNum, System.Action<NumerCappedRange> callNumChanged)
        {
            this.ClearData();
            this.SetData(strFmtNum, limitNum, callNumChanged);
            if (!limitNum.isHasRange)
            {
                this.Hide();
                return;
            }
            this.Show();
            this.ShowNumText();
            this.ShowSlider();
        }

        private void ClearData()
        {
            this.limitNum?.Put2Pool();
            this.strFmtNum = null;
            this.limitNum = null;
            this.funcNumChanged = null;
        }

        private void SetData(string strFmtNum, NumerCappedRange limitNum, System.Action<NumerCappedRange> callNumChanged)
        {
            this.strFmtNum = strFmtNum;
            this.limitNum = limitNum;
            this.funcNumChanged = callNumChanged;
        }

        private void ShowNumText()
        {
            this.ui.textUseNum.text = string.Format(strFmtNum, limitNum.current, limitNum.max);
        }

        private void ShowSlider()
        {
            this.ui.slider.SetNumberCapped(this.limitNum);
        }

        private void OnSliderValueChanged(float sliderValue)
        {
            int current = Math.Max((int)sliderValue, 1);
            bool isChanged = this.limitNum.SetCurrentReturnIsChanged(current);
            if (isChanged)
            {
                this.ExecuteSliderChanged();
            }
        }

        private void ExecuteSliderChanged()
        {
            this.ShowNumText();
            this.ExecuteOutCall();
        }

        private void ExecuteOutCall()
        {
            this.funcNumChanged?.Invoke(this.limitNum);
        }

        private void CalcSlilder(bool isAdd)
        {
            int changeValue = (isAdd ? 1 : -1);
            if (this.limitNum.AddCurrentReturnIsChanged(changeValue))
            {
                this.ExecuteSliderChanged();
                this.ShowSlider();
            }
        }

        private void OnClickAdd()
        {
            this.CalcSlilder(true);
        }

        private void OnClickReduce()
        {
            this.CalcSlilder(false);
        }
        #endregion methonds
    }
}
